/*
File:		Player.cpp
Author:		Ben Feldmann
Purpose:	Class for managing game player
*/

#include "Player.h"
#include "Game.h"

#include "Hazard.h"
#include "FloorSpikes.h"

// Weapons
#include "BaseGun.h"
#include "EMPGun.h"
#include "RocketGun.h"
#include "FlamethrowerGun.h"
#include "BaseMelee.h"
#include "LaserSword.h"
#include "Saw.h"
#include "Claw.h"

#include "Animation.h"
#include "Event.h"
#include "GamePlayState.h"
#include "MainMenuState.h"
#include "Tile.h"
#include "CreatePickUpMessage.h"

#include "SGD Wrappers/CSGD_TextureManager.h"
#include "SGD Wrappers/CSGD_DirectInput.h"
#include "SGD Wrappers/CSGD_Direct3D.h"
#include "EventSystem.h"

#include "Emitter.h"
#include "EmitterManager.h"


Player::Player() : Activate(false)
{
	type = OBJ_PLAYER;
	//SetX(200.0f);
	//SetY(100.0f);
	SetWidth(64);
	SetHeight(64);
	SetDirection(1);
	//SetTextureID(CSGD_TextureManager::GetInstance()->LoadTexture(_T("Graphics/Dummy Images/player.png")));

	prevX = GetX();
	prevY = GetY();
	collided = false;
	roomNum = -1;

	stun = false;
	stunTime = 0.0f;

	deathTimer = 0;
	deathSpan = 2.75f;

	//player.LoadAnimationFromFile("Animation.xml");

	health				= 550;
	maxHealth			= 550;
	energy				= 100;
	maxEnergy			= 100;

	moveSpeed			= 125.0f;
	isMoving			= false;

	scrapMetal			= 500;
	circuitry			= 500;

	// Variables for Skills
	skillOne			= -1;//CLOCK;
	skillTwo			= -1;//SHIELD;
	s1Active			= false;
	s2Active			= false;	

	camo				= false;
	clock				= false;
	shield				= false;
	skillTimer			= 0.0f;

	playerNum			= -1;
	canMove				= true;

	Animation* animation[4];

	for(int i = 0; i < 4; ++i)
	{
		animation[i] = new Animation;
		animation[i]->LoadAnimationFromFile("player_ClawAttack.xml", i);
		fistID[i] = AnimationSystem::GetInstance()->AddAnimation(animation[i]);
	}

	for(int i = 0; i < 4; ++i)
	{
		animation[i] = new Animation;
		animation[i]->LoadAnimationFromFile("player_ClawAttack.xml", i);
		clawID[i] = AnimationSystem::GetInstance()->AddAnimation(animation[i]);
	}

	for(int i = 0; i < 4; ++i)
	{
		animation[i] = new Animation;
		animation[i]->LoadAnimationFromFile("player_SawAttack.xml", i);
		sawID[i] = AnimationSystem::GetInstance()->AddAnimation(animation[i]);
	}

	for(int i = 0; i < 4; ++i)
	{
		animation[i] = new Animation;
		animation[i]->LoadAnimationFromFile("player_SwordAttack.xml", i);
		swordID[i] = AnimationSystem::GetInstance()->AddAnimation(animation[i]);
	}

	Animation* tmp = new Animation;
	tmp->LoadAnimationFromFile("player_SwordSpin.xml");
	swordSpinID = AnimationSystem::GetInstance()->AddAnimation(tmp);

	curWeapon			= 1;
	weapon1				= new BaseMelee(this);//new LaserSword(this);
	weapon1->AddRef();
	weapon2				= new BaseGun(this);
	weapon2->AddRef();

	weapon1->SetAnimationIDs(fistID);

	EventSystem::GetInstance()->RegisterClient("pickup_hit", this);
	EventSystem::GetInstance()->RegisterClient("player_hit", this);
	EventSystem::GetInstance()->RegisterClient("set_room", this);

	camoParticle   = Emitter::LoadEmitter("Emitters/camoParticle.xml");
	clockParticle  = Emitter::LoadEmitter("Emitters/clockParticle.xml");
	shieldParticle = Emitter::LoadEmitter("Emitters/shieldParticle.xml");

	for(int i = 0; i < 10; i++)
	{
		purchased[i] = 0;
	}

	fistAttack		= CSGD_XAudio2::GetInstance()->SFXLoadSound(_T("Sound/Melee Weapons/fistAttack.wav"));
	swordAttack1	= CSGD_XAudio2::GetInstance()->SFXLoadSound(_T("Sound/Melee Weapons/swordAttack1.wav"));
	swordAttack2	= CSGD_XAudio2::GetInstance()->SFXLoadSound(_T("Sound/Melee Weapons/swordAttack2.wav"));
	swordSpcAttack	= CSGD_XAudio2::GetInstance()->SFXLoadSound(_T("Sound/Melee Weapons/swordSpcAttack.wav"));
	sawAttack		= CSGD_XAudio2::GetInstance()->SFXLoadSound(_T("Sound/Melee Weapons/sawAttack.wav"));
	clawAttack		= CSGD_XAudio2::GetInstance()->SFXLoadSound(_T("Sound/Melee Weapons/clawAttack.wav"));
	clawAttach		= CSGD_XAudio2::GetInstance()->SFXLoadSound(_T("Sound/Melee Weapons/clawAttach.wav"));
	throwAttack		= CSGD_XAudio2::GetInstance()->SFXLoadSound(_T("Sound/Melee Weapons/throwAttack.wav"));

	laserShot		= CSGD_XAudio2::GetInstance()->SFXLoadSound(_T("Sound/Ranged Weapons/laserShot.wav"));
	rocketShot		= CSGD_XAudio2::GetInstance()->SFXLoadSound(_T("Sound/Ranged Weapons/rocketShot.wav"));
	empShot			= CSGD_XAudio2::GetInstance()->SFXLoadSound(_T("Sound/Ranged Weapons/empShot.wav"));
	flameShot		= CSGD_XAudio2::GetInstance()->SFXLoadSound(_T("Sound/Ranged Weapons/flameShot.wav"));

	walking			= CSGD_XAudio2::GetInstance()->SFXLoadSound(_T("Sound/Rob_Walk.wav"));
	hurt			= CSGD_XAudio2::GetInstance()->SFXLoadSound(_T("Sound/player_hurt.wav"));
	//death			= CSGD_XAudio2::GetInstance()->SFXLoadSound(_T("Sound/robot_explode.wav"));
	fail			= CSGD_XAudio2::GetInstance()->SFXLoadSound(_T("Sound/FailBeep.wav"));
	useSkill		= CSGD_XAudio2::GetInstance()->SFXLoadSound(_T("Sound/useSkill.wav"));

	deathEm			= Emitter::LoadEmitter("Emitters/enemyDeath.xml");
	em = EmitterManager::GetInstance();

}

Player::~Player()
{
	if(GetTextureID() != -1 )
	{
		CSGD_TextureManager::GetInstance()->UnloadTexture(GetTextureID());
		SetTextureID(-1);
	}

	EventSystem::GetInstance()->UnregisterClientAll(this);

	if( weapon1 )
	{
		weapon1->SetOwner(nullptr);
		weapon1->Release();
	}
	if( weapon2 )
	{
		weapon2->SetOwner(nullptr);
		weapon2->Release();
	}

	camoParticle->Shutdown();
	delete camoParticle;

	clockParticle->Shutdown();
	delete clockParticle;

	shieldParticle->Shutdown();
	delete shieldParticle;

	// Unload Sounds
	if(fail != -1)
	{
		CSGD_XAudio2::GetInstance()->SFXStopSound(fail);
		CSGD_XAudio2::GetInstance()->SFXUnloadSound(fail);
		fail = -1;
	}

	/*if(death != -1)
	{
		CSGD_XAudio2::GetInstance()->SFXStopSound(death);
		CSGD_XAudio2::GetInstance()->SFXUnloadSound(death);
		death = -1;
	}*/

	if(hurt != -1)
	{
		CSGD_XAudio2::GetInstance()->SFXStopSound(hurt);
		CSGD_XAudio2::GetInstance()->SFXUnloadSound(hurt);
		hurt = -1;
	}

	if(walking != -1)
	{
		CSGD_XAudio2::GetInstance()->SFXStopSound(walking);
		CSGD_XAudio2::GetInstance()->SFXUnloadSound(walking);
		walking = -1;
	}

	if(flameShot != -1)
	{
		CSGD_XAudio2::GetInstance()->SFXStopSound(flameShot);
		CSGD_XAudio2::GetInstance()->SFXUnloadSound(flameShot);
		flameShot = -1;
	}

	if(empShot != -1)
	{
		CSGD_XAudio2::GetInstance()->SFXStopSound(empShot);
		CSGD_XAudio2::GetInstance()->SFXUnloadSound(empShot);
		empShot = -1;
	}
	
	if(rocketShot != -1)
	{
		CSGD_XAudio2::GetInstance()->SFXStopSound(rocketShot);
		CSGD_XAudio2::GetInstance()->SFXUnloadSound(rocketShot);
		rocketShot = -1;
	}

	if(laserShot != -1)
	{
		CSGD_XAudio2::GetInstance()->SFXStopSound(laserShot);
		CSGD_XAudio2::GetInstance()->SFXUnloadSound(laserShot);
		laserShot = -1;
	}
	
	if(throwAttack != -1)
	{
		CSGD_XAudio2::GetInstance()->SFXStopSound(throwAttack);
		CSGD_XAudio2::GetInstance()->SFXUnloadSound(throwAttack);
		throwAttack = -1;
	}
	if(clawAttach != -1)
	{
		CSGD_XAudio2::GetInstance()->SFXStopSound(clawAttach);
		CSGD_XAudio2::GetInstance()->SFXUnloadSound(clawAttach);
		clawAttach = -1;
	}

	if(clawAttack != -1)
	{
		CSGD_XAudio2::GetInstance()->SFXStopSound(clawAttack);
		CSGD_XAudio2::GetInstance()->SFXUnloadSound(clawAttack);
		clawAttack = -1;
	}

	if(sawAttack != -1)
	{
		CSGD_XAudio2::GetInstance()->SFXStopSound(sawAttack);
		CSGD_XAudio2::GetInstance()->SFXUnloadSound(sawAttack);
		sawAttack = -1;
	}

	if(swordSpcAttack != -1)
	{
		CSGD_XAudio2::GetInstance()->SFXStopSound(swordSpcAttack);
		CSGD_XAudio2::GetInstance()->SFXUnloadSound(swordSpcAttack);
		swordSpcAttack = -1;
	}

	if(swordAttack2 != -1)
	{
		CSGD_XAudio2::GetInstance()->SFXStopSound(swordAttack2);
		CSGD_XAudio2::GetInstance()->SFXUnloadSound(swordAttack2);
		swordAttack2 = -1;
	}

	if(swordAttack1 != -1)
	{
		CSGD_XAudio2::GetInstance()->SFXStopSound(swordAttack1);
		CSGD_XAudio2::GetInstance()->SFXUnloadSound(swordAttack1);
		swordAttack1 = -1;
	}

	if(fistAttack != -1)
	{
		CSGD_XAudio2::GetInstance()->SFXStopSound(fistAttack);
		CSGD_XAudio2::GetInstance()->SFXUnloadSound(fistAttack);
		fistAttack = -1;
	}
	if(useSkill != -1)
	{
		CSGD_XAudio2::GetInstance()->SFXStopSound(useSkill);
		CSGD_XAudio2::GetInstance()->SFXUnloadSound(useSkill);
		useSkill = -1;
	}
}

void Player::UseSkill(int skill) 
{
	// Turn on the appropriate skill
	if(skill == CAMO)
	{
		camo = true;
		camoParticle->Loop(true);
		CSGD_XAudio2::GetInstance()->SFXPlaySound(useSkill);										
	}
	else if(skill == CLOCK)
	{
		clock = true;

		moveSpeed = 175.0f;

		// Bug Fix #41 Start - Brandon Jones
		/*while(health < maxHealth)
		{
			health += 3.0f;
		}*/
		// Bug Fix #41 End - Brandon Jones

		clockParticle->Loop(true);
		CSGD_XAudio2::GetInstance()->SFXPlaySound(useSkill);										
	}
	else if(skill == SHIELD)
	{
		shield = true;

		shieldParticle->Loop(true);
		CSGD_XAudio2::GetInstance()->SFXPlaySound(useSkill);										
	}
}

void Player::Attack() 
{
	if(curWeapon == 1)
	{
		if(weapon1->IsAttacking() == false)
		{
			weapon1->Attack();
			PlaySound(weapon1->GetMeleeType());
		}
	}
	else if(curWeapon == 2)
	{
		if(weapon2->isAttackReady())
		{
			weapon2->Attack();
			PlaySound(weapon2->GetRangeType());
		}
	}
}

void Player::SpecialAttack() 
{
	if(curWeapon == 1)
	{
		if(energy >= weapon1->GetEnergyCost() && weapon1->IsSpecialReady())
		{
			weapon1->SpecialAttack();
			PlaySpcSound(weapon1->GetMeleeType());			
		}
		else
		{
			PlaySound(3, false);
		}
	}
	else if(curWeapon == 2)
	{
		if(energy >= weapon2->GetEnergyCost())
		{
			//canMove = false;
			if(weapon2->isSpecialReady())
			{
				weapon2->SpecialAttack();
				PlaySpcSound(weapon2->GetRangeType());
				energy -= weapon2->GetEnergyCost();
			}
			else
			{
				PlaySound(3, false);
			}
		}
		else
		{
			PlaySound(3, false);
		}
	}
}

void Player::Update(float elapsed)
{
	if(health <= 1)
		health = 1;

	if(health == 1)
	{
		if(deathTimer == 0)
		{
			PlaySound(2, false);
			deathAnimation[GetDirection()].Play();
		}
		
		deathAnimation[GetDirection()].Update(elapsed);
		deathEm->SetPosition(GetX()+(GetWidth()/2.0f), GetY()+(GetHeight()/2.0f));

		if(deathAnimation[GetDirection()].GetCurFrame() == 4)
		{
			if(deathAnimation[GetDirection()].IsPlaying())
			{
				deathAnimation[GetDirection()].Stop();
				em->FireBurstEmitter(deathEm);
			}

			deathEm->Update(elapsed);
		}

		deathTimer += elapsed;

		if(deathTimer >= deathSpan)
			health = 0;

		return;
	}

	// If energy is less than zero, turn off all skills
	if(energy <= 0)
	{
		s1Active = false;
		s2Active = false;
		camo   = false;
		clock  = false;
		shield = false;

		moveSpeed = 100.0f;

		energy = 0.0f;

		camoParticle->Loop(false);
		clockParticle->Loop(false);
		shieldParticle->Loop(false);
	}

	CSGD_DirectInput* di = CSGD_DirectInput::GetInstance();

	// Movement

	/* CARDINAL */

	if(stun == true)
	{
		stunTime += elapsed;

		if(stunTime >= 5.0f)
			stun = false;
		else
			return;
	}

	if(canMove == true)
	{
		
		// North
		if(di->JoystickGetLStickYAmount(playerNum == 1 ? 0 : 1) < -500 && di->JoystickGetLStickXNormalized(playerNum == 1 ? 0 : 1) < .5f && di->JoystickGetLStickXNormalized(playerNum == 1 ? 0 : 1) > -.5f && di->JoystickGetLStickYNormalized(playerNum == 1 ? 0 : 1) < 0.0f)
		{	
			SetY(GetY()-moveSpeed*elapsed); SetDirection(3); isMoving = true;
			if(!moveAnimation[3].IsPlaying())
			{
				moveAnimation[3].Play();
				PlaySound(0, false);
			}
		}
		// East
		else if(di->JoystickGetLStickXAmount(playerNum == 1 ? 0 : 1) > 500 && di->JoystickGetLStickYNormalized(playerNum == 1 ? 0 : 1) > -.5f && di->JoystickGetLStickYNormalized(playerNum == 1 ? 0 : 1) < .5f && di->JoystickGetLStickXNormalized(playerNum == 1 ? 0 : 1) > 0.0f)
		{	
			SetX(GetX()+moveSpeed*elapsed); SetDirection(0); isMoving = true;
			if(!moveAnimation[0].IsPlaying())
			{
				moveAnimation[0].Play();
				PlaySound(0, false);
			}
		}
		// South
		else if(di->JoystickGetLStickYAmount(playerNum == 1 ? 0 : 1) > 500 && di->JoystickGetLStickXNormalized(playerNum == 1 ? 0 : 1) > -.5f && di->JoystickGetLStickXNormalized(playerNum == 1 ? 0 : 1) < .5f && di->JoystickGetLStickYNormalized(playerNum == 1 ? 0 : 1) > 0.0f)
		{	
			SetY(GetY()+moveSpeed*elapsed); SetDirection(2);  isMoving = true;
			//int n = di->JoystickGetLStickYAmount();

			if(!moveAnimation[2].IsPlaying())
			{
				moveAnimation[2].Play();
				PlaySound(0, false);
			}
		}
		// West
		else if(di->JoystickGetLStickXAmount(playerNum == 1 ? 0 : 1) < -500 && di->JoystickGetLStickYNormalized(playerNum == 1 ? 0 : 1) < .5f && di->JoystickGetLStickYNormalized(playerNum == 1 ? 0 : 1) > -.5f && di->JoystickGetLStickXNormalized(playerNum == 1 ? 0 : 1) < 0.0f)
		{	
			SetX(GetX()-moveSpeed*elapsed); SetDirection(1); isMoving = true;
			if(!moveAnimation[1].IsPlaying())
			{
				moveAnimation[1].Play();
				PlaySound(0, false);
			}
		}

		//////////////////////

		/* ORDINAL */

		//Northeast
		else if(di->JoystickGetLStickXNormalized(playerNum == 1 ? 0 : 1) > .5f && di->JoystickGetLStickYNormalized(playerNum == 1 ? 0 : 1) < -.5f &&
				di->JoystickGetLStickXAmount(playerNum == 1 ? 0 : 1) > 300 && di->JoystickGetLStickYAmount(playerNum == 1 ? 0 : 1) < -300)
		{
			SetY(GetY()-(moveSpeed/2)*elapsed); SetX(GetX()+(moveSpeed/2)*elapsed); SetDirection(0); isMoving = true;
			if(!moveAnimation[0].IsPlaying())
			{
				moveAnimation[0].Play();
				PlaySound(0, false);
			}
		}
		// Southeast
		else if(di->JoystickGetLStickXNormalized(playerNum == 1 ? 0 : 1) > .5f && di->JoystickGetLStickYNormalized(playerNum == 1 ? 0 : 1) > .5f &&
				di->JoystickGetLStickXAmount(playerNum == 1 ? 0 : 1) > 300 && di->JoystickGetLStickYAmount(playerNum == 1 ? 0 : 1) > 300)
		{
			SetY(GetY()+(moveSpeed/2)*elapsed); SetX(GetX()+(moveSpeed/2)*elapsed); SetDirection(0); isMoving = true;
			if(!moveAnimation[0].IsPlaying())
			{
				moveAnimation[0].Play();
				PlaySound(0, false);
			}
		}
		// SouthWest
		else if(di->JoystickGetLStickXNormalized(playerNum == 1 ? 0 : 1) < -.5f && di->JoystickGetLStickYNormalized(playerNum == 1 ? 0 : 1) > .5f &&
				di->JoystickGetLStickXAmount(playerNum == 1 ? 0 : 1) < -300 && di->JoystickGetLStickYAmount(playerNum == 1 ? 0 : 1) > 300)
		{
			SetY(GetY()+(moveSpeed/2)*elapsed); SetX(GetX()-(moveSpeed/2)*elapsed); SetDirection(1); isMoving = true;
			if(!moveAnimation[1].IsPlaying())
			{
				moveAnimation[1].Play();
				PlaySound(0, false);
			}
		}
		// Northwest
		else if(di->JoystickGetLStickXNormalized(playerNum == 1 ? 0 : 1) < -.5f && di->JoystickGetLStickYNormalized(playerNum == 1 ? 0 : 1) < .5f &&
				di->JoystickGetLStickXAmount(playerNum == 1 ? 0 : 1) < -300 && di->JoystickGetLStickYAmount(playerNum == 1 ? 0 : 1) < -300)
		{
			SetY(GetY()-(moveSpeed/2)*elapsed); SetX(GetX()-(moveSpeed/2)*elapsed); SetDirection(1); isMoving = true;
			if(!moveAnimation[1].IsPlaying())
			{
				moveAnimation[1].Play();
				PlaySound(0, false);
			}
		}
		else
		{
			for(int i = 0; i < 4; ++i)
			{
				if(moveAnimation[i].IsPlaying())
				{
					moveAnimation[i].Stop();
					if(CSGD_XAudio2::GetInstance()->SFXIsSoundPlaying(walking))
						CSGD_XAudio2::GetInstance()->SFXStopSound(walking);
				}
			}
			 isMoving = false;
		}

	}


	//////////////////////////////////////////////////////////////


	// Actions
	//A = 0
	//B = 1
	//X = 2
	//Y = 3

	/* Attack */
	//if(curWeapon == 1)
	//{
	//	if(di->JoystickButtonPressed(0, playerNum == 1 ? 0 : 1))
	//	{
	//		if( CanActivate() == false )
	//			Attack();
	//	}
	//}
	//else
	//{
		if(di->JoystickButtonDown(0, playerNum == 1 ? 0 : 1))
		{
			if( CanActivate() == false )
				Attack();
		}
	//}

	/* Special Attack */
	if(di->JoystickButtonPressed(2, playerNum == 1 ? 0 : 1))
	{
		SpecialAttack();
	}

	/* Switch Weapons */
	if(di->JoystickButtonPressed(1, playerNum == 1 ? 0 : 1))
	{
		if(curWeapon == 1)
			curWeapon = 2;
		else if(curWeapon == 2)
			curWeapon = 1;
	}

	/* Skill One */
	if(di->JoystickButtonPressed(4, playerNum == 1 ? 0 : 1))
	{
		camoParticle->Loop(false);
		clockParticle->Loop(false);
		shieldParticle->Loop(false);

		if(s1Active == false && s2Active == false)	// If neither skill is on, turn on s1
		{
			s1Active = true;
			// Turn off all skills and let USESKILL to turn on appropriate skill
			camo   = false;
			clock  = false;
			shield = false;
			moveSpeed = 100.0f;
			UseSkill(skillOne);
		}
		else if(s1Active == false && s2Active == true)	// If s2 is on, then turn it off and turn on s1
		{
			s2Active = false;
			s1Active = true;
			// Turn off all skills and let USESKILL to turn on appropriate skill
			camo   = false;
			clock  = false;
			shield = false;
			moveSpeed = 100.0f;
			UseSkill(skillOne);
		}
		else if(s1Active == true && s2Active == false)	// If s1 is on, turn it off
		{
			s1Active = false;
			// Turn off all skills
			camo   = false;
			clock  = false;
			shield = false;
			moveSpeed = 100.0f;
		}
	}

	/* Skill Two */
	if(di->JoystickButtonPressed(5, playerNum == 1 ? 0 : 1))
	{
		if(s2Active == false && s1Active == false)	// If neither skill is on, turn on s2
		{
			s2Active = true;
			// Turn off all skills and let USESKILL to turn on appropriate skill
			camo   = false;
			clock  = false;
			shield = false;
			moveSpeed = 100.0f;
			UseSkill(skillTwo);
		}
		else if(s2Active == false && s1Active == true)	// If s1 is on, then turn it off and turn on s2
		{
			s1Active = false;
			s2Active = true;
			// Turn off all skills and let USESKILL to turn on appropriate skill
			camo   = false;
			clock  = false;
			shield = false;
			moveSpeed = 100.0f;
			UseSkill(skillTwo);
		}
		else if(s2Active == true && s1Active == false)	// If s2 is on, turn it off
		{
			s2Active = false;
			// Turn off all skills
			camo   = false;
			clock  = false;
			shield = false;
			moveSpeed = 100.0f;
		}
	}

	//////////////////////////////////////////////////////////////

	// Update Energy if Skill is being used
	if(camo == true)
	{
		skillTimer += elapsed;

		if(skillTimer >= 1.0f)
		{
			energy -= 10;
			skillTimer = 0.0f;
		}
	}
	else if(clock == true)
	{
		skillTimer += elapsed;

		if(skillTimer >= 1.0f)
		{
			energy -= 10;
			skillTimer = 0.0f;
		}
	}


	// Check Wall Collision
	if(collided == false)
	{
		prevX = GetX();
		prevY = GetY();
	}
	collided = false;

	player->Update(elapsed);
	moveAnimation[GetDirection()].Update(elapsed);

	// Recharge energy if applicable
	if(energy < maxEnergy && energy >= 0 && s1Active == false && s2Active == false)
		energy += 2*elapsed;


	// Update Particles
	if(camo == true)
	{
		camoParticle->Update(elapsed);
		camoParticle->SetPosition((GetX()+GetWidth()/2.0f/* - GamePlayState::GetInstance()->GetCamX()*/), (GetY()+GetHeight()/2.0f)/* - GamePlayState::GetInstance()->GetCamY()*/);
	}
	if(clock == true)
	{
		clockParticle->Update(elapsed);
		clockParticle->SetPosition((GetX()+GetWidth()/2.0f)/* - GamePlayState::GetInstance()->GetCamX()*/, (GetY()+GetHeight()/2.0f)/* - GamePlayState::GetInstance()->GetCamY()*/);
	}
	if(shield == true)
	{
		shieldParticle->Update(elapsed);
		shieldParticle->SetPosition((GetX()+GetWidth()/2.0f)/* - GamePlayState::GetInstance()->GetCamX()*/, (GetY()-15)/* - GamePlayState::GetInstance()->GetCamY()*/);
	}

	/*if(health <= 0)
		health = 0;*/

	CanActivate(false);
}

void Player::Render()
{
	if(health == 1)
	{
		deathAnimation[GetDirection()].Render(int(GetX()- GamePlayState::GetInstance()->GetCamX()),
			int(GetY() - GamePlayState::GetInstance()->GetCamY()), 1.0f, D3DCOLOR_ARGB(255, 255, 255, 255), false);

		return;
	}

	if(curWeapon == 1 && (weapon1->IsAttacking() || weapon1->IsSpcAttacking()))
	{
		// Bug Fix #43 Start - Brandon Jones
		if(weapon1->GetMeleeType() == MW_SWORD && dynamic_cast<LaserSword*>(weapon1)->IsBerserking())
		{
			player->Render(int(GetX()- GamePlayState::GetInstance()->GetCamX()),
				int(GetY() - GamePlayState::GetInstance()->GetCamY()), 1.0f,
				camo == true ? D3DCOLOR_ARGB(127, 255, 127, 127) : D3DCOLOR_ARGB(255, 255, 127, 127), false);
		}
		else
		{
			player->Render(int(GetX()- GamePlayState::GetInstance()->GetCamX()),
				int(GetY() - GamePlayState::GetInstance()->GetCamY()), 1.0f,
				camo == true ? D3DCOLOR_ARGB(127, 255, 255, 255) : D3DCOLOR_ARGB(255, 255, 255, 255), false);
		}
	}
	else if(isMoving)
	{
		if(weapon1->GetMeleeType() == MW_SWORD && dynamic_cast<LaserSword*>(weapon1)->IsBerserking())
		{
			moveAnimation[GetDirection()].Render(int(GetX()- GamePlayState::GetInstance()->GetCamX()),
				int(GetY() - GamePlayState::GetInstance()->GetCamY()), 1.0f,
				camo == true ? D3DCOLOR_ARGB(127, 255, 127, 127) : D3DCOLOR_ARGB(255, 255, 127, 127), false);
		}
		else
		{
			moveAnimation[GetDirection()].Render(int(GetX()- GamePlayState::GetInstance()->GetCamX()),
				int(GetY() - GamePlayState::GetInstance()->GetCamY()), 1.0f,
				camo == true ? D3DCOLOR_ARGB(127, 255, 255, 255) : D3DCOLOR_ARGB(255, 255, 255, 255), false);
		}
	}
	else
	{
		if(weapon1->GetMeleeType() == MW_SWORD && dynamic_cast<LaserSword*>(weapon1)->IsBerserking())
		{
			idleAnimation[GetDirection()].Render(int(GetX()- GamePlayState::GetInstance()->GetCamX()),
				int(GetY() - GamePlayState::GetInstance()->GetCamY()), 1.0f,
				camo == true ? D3DCOLOR_ARGB(127, 255, 127, 127) : D3DCOLOR_ARGB(255, 255, 127, 127), false);
		}
		else
		{
			idleAnimation[GetDirection()].Render(int(GetX()- GamePlayState::GetInstance()->GetCamX()),
				int(GetY() - GamePlayState::GetInstance()->GetCamY()), 1.0f,
				camo == true ? D3DCOLOR_ARGB(127, 255, 255, 255) : D3DCOLOR_ARGB(255, 255, 255, 255), false);
		}
		// Bug Fix #43 End

		//CSGD_Direct3D::GetInstance()->GetSprite()->Flush();
		//CSGD_Direct3D::GetInstance()->DrawRect(GetRect(), 255, 0, 0);
	}
	if(camo == true)
		camoParticle->Render();

	if(clock == true)
	{
		clockParticle->Render();
	}
	if(shield == true)
	{
		shieldParticle->Render();
	}

	

	//player.Render(int(GetX()- GamePlayState::GetInstance()->GetCamX()), int(GetY() - GamePlayState::GetInstance()->GetCamY()), 1.0f, D3DCOLOR_XRGB(255, 255, 255), false);

	//CSGD_Direct3D::GetInstance()->GetSprite()->Flush();
}

bool Player::CheckCollision(IEntity* base) 
{
	if(Entity::CheckCollision(base) == true && base->GetType() != OBJ_PICKUP && base->GetType() != OBJ_WEAPON && base->GetType() != OBJ_BULLET)
	{
		/*if(base->GetType() == OBJ_TILE)
		roomNum = dynamic_cast<Tile*>(base)->GetRoomNum();*/
		if(base->GetType() == OBJ_TILE)
		{
			Tile* t = dynamic_cast<Tile*>(base);
			if(t->GetTileType() == TILE_TRIGGER)
			{
				if(t->GetEvent() == "set_room")
					SetRoomNum(t->GetRoomNum());
			}
			if(t->GetSolid() == false)
			{
				collided = false;
				return false;
			}
		}
		if(base->GetType() == OBJ_PLAYER)
		{
			collided = false;
			return false;
		}
		// Spike collision
		if( base->GetType() == OBJ_HAZARD )
		{
			if( ((Hazard*)base)->GetHazardType() == Hazard::HZ_SPIKES )
			{
				if( ((FloorSpikes*)base)->IsUp() )
				{
					collided = true;
					SetX(prevX);
					SetY(prevY);
					return true;
				}
				else
				{
					collided = false;
					return false;
				}
			}
		}
		
		{
			SetX(prevX); 
			SetY(prevY); 
			collided = true;
			return true;
		}
	}
	else
	{
		collided = false;
		return false;
	}
}

void Player::HandleEvent(Event* thisEvent)
{
	if(thisEvent->GetEventID() == "garbage")
		return;

	if(thisEvent->GetEventID() == "pickup_hit")
	{
		void* tmpPtr = thisEvent->GetParameter();
		int* tmp = (int*)(tmpPtr);

		void* tmpPtr2 = thisEvent->GetParameter2();
		int* tmp2 = (int*)(tmpPtr2);

		if(*tmp == 0)
			scrapMetal += *tmp2;
		else
			circuitry  += *tmp2;

		delete tmp;
		delete tmp2;

		thisEvent->SetEventID("garbage");

		return;
	}

	if(thisEvent->GetEventID() == "player_hit")
	{
		if(*((int*)thisEvent->GetParameter3()) == playerNum)
		{
			void* tmpPtr = thisEvent->GetParameter2();
			float* tmp = (float*)(tmpPtr);
			if(thisEvent->GetParameter() == this)
			{
				if(shield == true)
					energy -= *tmp;
				else
					health -= *tmp;
			}

			PlaySound(1, false);

			if(health <= 1)
				health = 1;

			delete tmp;
			thisEvent->SetEventID("garbage");
			return;
		}
	}

	if(thisEvent->GetEventID() == "set_room")
	{

	}
}

RECT Player::GetRect()
{
	RECT tmp = idleAnimation[GetDirection()].GetCollsionRect();
	int width = tmp.right;
	int height = tmp.bottom;
	tmp.top += (long)(GetY());
	tmp.bottom = height + tmp.top + 5;
	tmp.left += (long)(GetX());
	tmp.right = width + tmp.left + 5;

	switch(GetDirection())
	{
	case 0:
		tmp.top += 5;
		tmp.right += 10;
		break;

	case 1:
		tmp.top += 5;
		tmp.right -= 15;
		tmp.left -= 5;
		break;

	case 2:
		tmp.left += 5;
		tmp.right -= 5;
		tmp.top += 10;
		break;

	case 3:
		tmp.right -= 8;
		tmp.bottom -= 10;
		break;
	}

	return tmp;
}

void Player::SetMoveAnimation(int id[4])
{
	for(int i = 0; i < 4; ++i)
	{
		moveAnimation[i] = *AnimationSystem::GetInstance()->GetAnimation(id[i]);
	}

	//SetWidth(moveAnimation[0].GetCollsionRect().right - moveAnimation[0].GetCollsionRect().left);
	//SetHeight(moveAnimation[0].GetCollsionRect().bottom - moveAnimation[0].GetCollsionRect().top);
}

void Player::SetIdleAnimation(int id[4])
{
	for(int i = 0; i < 4; ++i)
	{
		idleAnimation[i] = *AnimationSystem::GetInstance()->GetAnimation(id[i]);
	}

	SetWidth(idleAnimation[0].GetCollsionRect().right);
	SetHeight(idleAnimation[0].GetCollsionRect().bottom);
}

void Player::SetDeathAnimation(int id[4])
{
	for(int i = 0; i < 4; ++i)
	{
		deathAnimation[i] = *AnimationSystem::GetInstance()->GetAnimation(id[i]);
	}
}

void Player::SetAnimation(Animation* tmp)
{
	player = tmp;

	//SetWidth(player->GetCollsionRect().right - player->GetCollsionRect().left);
	//SetHeight(player->GetCollsionRect().bottom - player->GetCollsionRect().top);

	weapon2->SetX(GetX() + GetWidth());
	weapon2->SetY(GetY());
}

int Player::GetMeleeWeaponType()
{
	int t = weapon1->GetMeleeType();

	return t;
}

int Player::GetRangedWeaponType()
{
	int t = weapon2->GetRangeType();

	return t;
}

void Player::SetEquipment(int melee, int ranged, int sk1, int sk2, int items[10])
{
	if(melee != GetMeleeWeaponType())
	{
		if(weapon1)
		{
			ObjectManager::GetInstance()->RemoveObject(weapon1);
			weapon1->Release();
			weapon1 = nullptr;
		}

		switch(melee)
		{
		case MW_BASE:
			{
				weapon1 = new BaseMelee(this);
				weapon1->SetAnimationIDs(fistID);
			}
			break;

		case MW_SWORD:
			{
				weapon1 = new LaserSword(this);
				weapon1->SetAnimationIDs(swordID);
				weapon1->SetSpecialAnimation(swordSpinID);
			}
			break;

		case MW_SAW:
			{
				weapon1 = new Saw(this);
				weapon1->SetAnimationIDs(sawID);
			}
			break;

		case MW_CLAW:
			{
				weapon1 = new Claw(this);
				weapon1->SetAnimationIDs(clawID);
			}
			break;
		}
		if(weapon1)
		{
			weapon1->AddRef();
		}
	}

	/*if(ranged != GetRangedWeaponType())
	{*/
		if( weapon2 )
		{
			ObjectManager::GetInstance()->RemoveObject(weapon2);
			weapon2->Release();
			weapon2 = nullptr;
		}

		switch(ranged)
		{
		case RW_BASE:
			{	

				weapon2 = new BaseGun(this);
			}
			break;

		case RW_ROCKET:
			{
				weapon2 = new RocketGun(this);
			}
			break;

		case RW_EMP:
			{
				weapon2 = new EMPGun(this);
			}
			break;

		case RW_FLAME:
			{
				weapon2 = new FlamethrowerGun(this);
			}
			break;
		}
		if( weapon2 )
			weapon2->AddRef();
	//}

	if(sk1 != skillOne)
	{
		switch(sk1)
		{
		case CAMO:
			{
				skillOne = CAMO;
			}
			break;

		case CLOCK:
			{
				skillOne = CLOCK;
			}
			break;

		case SHIELD:
			{
				skillOne = SHIELD;
			}
			break;
		}
	}

	if(sk2 != skillTwo)
	{
		switch(sk2)
		{
		case CAMO:
			{
				skillTwo = CAMO;
			}
			break;

		case CLOCK:
			{
				skillTwo = CLOCK;
			}
			break;

		case SHIELD:
			{
				skillTwo = SHIELD;
			}
			break;
		}
	}

	// Transfer purchases over to player
	for(int i = 0; i < 10; i++)
	{
		purchased[i] = items[i];
	}
}

void Player::PlaySound(int weapon, bool isWeapon)
{
	if(!isWeapon)
	{
		switch(weapon)
		{
		case 0:
			if(!CSGD_XAudio2::GetInstance()->SFXIsSoundPlaying(walking))
				CSGD_XAudio2::GetInstance()->SFXPlaySound(walking, true);
			break;

		case 1:
			CSGD_XAudio2::GetInstance()->SFXPlaySound(hurt);
			break;

		case 2:
			CSGD_XAudio2::GetInstance()->SFXPlaySound(GamePlayState::GetInstance()->GetExplodeSoundID());
			break;

		case 3:
			CSGD_XAudio2::GetInstance()->SFXPlaySound(fail);
			break;
		}
		return;
	}

	if(GetCurWeapon() == 1)
	{
		switch(weapon)
		{
			case MW_BASE:
				CSGD_XAudio2::GetInstance()->SFXPlaySound(fistAttack);
				break;

			case MW_SWORD:
			{
				int x = rand() % 2 + 1;
				if(x == 1)
					CSGD_XAudio2::GetInstance()->SFXPlaySound(swordAttack1);
				else
					CSGD_XAudio2::GetInstance()->SFXPlaySound(swordAttack2);					
			}
				break;

			case MW_SAW:
				CSGD_XAudio2::GetInstance()->SFXPlaySound(sawAttack);
				break;

			case MW_CLAW:
				CSGD_XAudio2::GetInstance()->SFXPlaySound(clawAttack);
				break;
		}
	}
	else
	{
		switch(weapon)
		{
			case MW_BASE:
				CSGD_XAudio2::GetInstance()->SFXPlaySound(laserShot);
				break;

			case RW_ROCKET:
				CSGD_XAudio2::GetInstance()->SFXPlaySound(rocketShot);
				break;

			case RW_EMP:
				CSGD_XAudio2::GetInstance()->SFXPlaySound(empShot);
				break;

			case RW_FLAME:
				CSGD_XAudio2::GetInstance()->SFXPlaySound(flameShot);
				break;
		}
	}
	
}

void Player::PlaySpcSound(int weapon)
{
	if(GetCurWeapon() == 1)
	{
		switch(weapon)
		{
			case MW_SWORD:
				CSGD_XAudio2::GetInstance()->SFXPlaySound(swordSpcAttack);				
				break;

			case MW_SAW:
				CSGD_XAudio2::GetInstance()->SFXPlaySound(throwAttack);
				break;

			case MW_CLAW:
				CSGD_XAudio2::GetInstance()->SFXPlaySound(throwAttack);
				break;
		}
	}
	else
	{
		switch(weapon)
		{
			case RW_ROCKET:
				CSGD_XAudio2::GetInstance()->SFXPlaySound(rocketShot);
				break;

			case RW_EMP:
				CSGD_XAudio2::GetInstance()->SFXPlaySound(empShot);
				break;

			case RW_FLAME:
				CSGD_XAudio2::GetInstance()->SFXPlaySound(flameShot);
				break;
		}
	}
}